﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XRLib.INPUTHANDLER
{
    public class InputHandler : GameComponent
    {
        static KeyboardState keyboardState;
        static KeyboardState lastKeyboardState;
        static MouseState mouseState;
        static MouseState lastmouseState;


        public static Vector2 mousePosition
        {
            get
            {
                Vector2 np = new Vector2(mouseState.X, mouseState.Y);
                return np;
            }
            set { }
        }

        public static MouseState MouseState
        {
            get { return mouseState; }
        }
        public static MouseState LastMouseState
        {
            get { return lastmouseState; }
        }

        public static KeyboardState KeyboardState
        {
            get { return keyboardState; }
        }
        public static KeyboardState LastKeyboardState
        {
            get { return lastKeyboardState; }
        }

        public InputHandler(Game game)
            : base(game)
        {
            keyboardState = Keyboard.GetState();

        }

        public override void Update(GameTime gameTime)
        {
            lastKeyboardState = keyboardState;
            lastmouseState = mouseState;

            mouseState = Mouse.GetState();
            keyboardState = Keyboard.GetState();
            base.Update(gameTime);
        }
        public static void Flush()
        {
            lastKeyboardState = keyboardState;
            lastmouseState = mouseState;
        }

        //the methods
        public static bool KeyReleased(Keys key)
        {
            return keyboardState.IsKeyUp(key) &&
                lastKeyboardState.IsKeyDown(key);
        }

        public static bool KeyPressed(Keys key)
        {
            return keyboardState.IsKeyDown(key) &&
                lastKeyboardState.IsKeyUp(key);
        }


        public static bool KeyDown(Keys key)
        {
            return keyboardState.IsKeyDown(key);
        }

        public static bool MouseReleased(ButtonState button)
        {
            bool op1 = false;
            bool op2 = false;

            if (mouseState.LeftButton != button)
                op1 = true;
            if (lastmouseState.LeftButton == button)
                op2 = true;

            return op1 && op2;
        }
        public static bool ScrollDown()
        {
            bool ret = false;
            if (lastmouseState.ScrollWheelValue > mouseState.ScrollWheelValue)
                ret = true;
            return ret;
        }
        public static bool ScrollUp()
        {
            bool ret = false;
            if (lastmouseState.ScrollWheelValue < mouseState.ScrollWheelValue)
                ret = true;
            return ret;
        }
        public static bool MousePressed(ButtonState button)
        {
            bool op1 = false;
            bool op2 = false;


            if (mouseState.LeftButton == button)
                op1 = true;
            if (lastmouseState.LeftButton != button)
                op2 = true;

            return op1 && op2;
        }
        public static bool MouseDown(ButtonState button)
        {
            bool op1 = false;
            if (mouseState.LeftButton == button)
                op1 = true;

            return op1;
        }

    }
}
